package com.bosssoft.oss.controller;

import com.aliyun.oss.OSSClient;
import com.bosssoft.oss.pojo.FileUpAndDown;
import com.bosssoft.oss.service.impl.FileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/oss/nothread/")
public class FileUpAndDownController {
    @Autowired
    private FileServiceImpl service;

    /**
     * 上传文件
     * @param uploadFile
     * @return
     * @throws Exception
     */
    @RequestMapping("upload")
    @ResponseBody
    public FileUpAndDown upload(@RequestParam("uploadFile") MultipartFile uploadFile) throws Exception {
        return service.upload(uploadFile);
    }

    /**
     * 下载文件
     * @param objectName
     * @param response
     * @throws IOException
     */
    @RequestMapping("download")
    @ResponseBody
    public void download(@RequestParam("objectName") String objectName, HttpServletResponse response, OSSClient ossClient,  String path) throws IOException {


        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(objectName.getBytes(), "ISO-8859-1"));
        service.download(ossClient,objectName,path);
    }

}
